/**
 * ****************************************************************************
 * Project: RoboBridge
 * ----------------------------------------------------------------------------
 * -------------------------- [c]2012 - KrawallMann ---------------------------
 * ---------------------- http://www.krawallmann.de.tc/ -----------------------
 * ****************************************************************************
 * File: TimedMultiState.java Package: tc.de.krawallmann.robobridge Version: 1.0
 * Author(s): KrawallMann
 * ****************************************************************************
 * Description:
 * ****************************************************************************
 */
package tc.de.krawallmann.robobridge;


/**
 * The Class TimedMultiState provides an easy was to schedule events with equal time
 * intervals.
 * 
 * @author KrawallMann
 * @version 1.0
 */
public class TimedMultiState extends MultiState {

	/** The last time. */
	transient long lastTime;

	/** The time between. */
	private final transient long timeBetween;

	/**
	 * Instantiates a new multi state.
	 * 
	 * @param states
	 *            the states
	 * @param timeBetween
	 *            the time between
	 */
	public TimedMultiState(final int states, final long timeBetween) {
		super(states);
		this.timeBetween = timeBetween;
	}
	

	@Override
	public void reset() {
		super.reset();
		lastTime = 0;
	}


	/**
	 * Update.
	 */
	public void update() {
		if (lastTime == 0) {
			lastTime = System.currentTimeMillis();
			for (final MultiStateListener l : listeners) {
				l.onStateChanged(state);
			}
		}
		if (System.currentTimeMillis() - lastTime >= timeBetween) {
			++state;
			state = state % (states + 1);
			for (final MultiStateListener l : listeners) {
				l.onStateChanged(state);
			}
			lastTime = System.currentTimeMillis();
		}
	}
}
